package edu.sdwu.liqing.qasystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.sdwu.liqing.qasystem.annotation.HasRole;
import edu.sdwu.liqing.qasystem.annotation.Login;
import edu.sdwu.liqing.qasystem.entity.QaModule;
import edu.sdwu.liqing.qasystem.model.Response;
import edu.sdwu.liqing.qasystem.service.IQaModuleService;
import edu.sdwu.liqing.qasystem.vo.QaModuleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ViewResolver;

import java.util.List;

/**
 * <p>
 *   前端控制器
 * </p>
 *
 * @author liqing@sdwu.edu
 * @since 2020-03-22
 */
@RestController
@RequestMapping("api/qa-module")
public class QaModuleController {

    @Autowired
    IQaModuleService moduleService;


    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    @PostMapping
    public Response<String> save(@RequestBody QaModule module) {
        if (moduleService.save(module)) {
            return Response.create(200, "新增成功", null);
        }
        return Response.create(500, "新增失败", null);
    }

    //可以更新自己提的，如果是管理员就可以全部更新
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    @PutMapping
    public Response<String> update(@RequestBody QaModule module) {
        if (moduleService.update(module)) {
            return Response.create(200, "更新成功", null);
        }
        return Response.create(500, "更新失敗", null);
    }

    //可以删自己提的，如果是管理员就可以全部删除
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    @DeleteMapping("{id}")
    public Response<String> delete(@PathVariable("id") String id) {
        if (moduleService.delete(id)) {
            return Response.create(200, "刪除成功", null);
        }
        return Response.create(500, "刪除失敗", null);
    }

    //查看只区分服务人员和医护人员
    @HasRole({"ADMIN", "DOC-NU", "SERVANT"})
    @PostMapping("page")
    public Response<Page<QaModule>> page(@RequestBody QaModuleVo vo) {
        return Response.ok(moduleService.page(vo));
    }


    @Login
    @GetMapping
    public Response<List<QaModule>> getList() {
        return Response.ok(moduleService.list());
    }

}

